<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author lz06
 */
class ItemDb {

    private $dbc;

    function __construct(DBC $dbc) {
        $this->dbc = $dbc;
    }

    /**
     * 获得商品列表
     * @param type $type
     * @param type $start
     * @param type $limit
     * @return type
     */
    public function getItem($type, $start, $limit,$job) {
		if(is_array( $job ))
		{
			$job1 = $job[0];
			$job2 = $job[1];
			$job3 = $job[2];
			$job4 = $job[3];
		}else
		{
			$job1 = $job;
			$job2 = $job;
			$job3 = $job;
			$job4 = $job;
		}
        if (!is_numeric($start)) {
            $start = 0;
        }
        if (!is_numeric($limit)) {
            $limit = 10;
        }
        if ($type == "all" || $type == "") {
			if ($job != -1)
			{
				$sql = "SELECT TOP " . $limit . " * FROM ItemInfo WHERE id_x NOT IN(SELECT TOP " . $start . " id_x FROM ItemInfo WHERE Status = 1) AND Status = 1 AND (job = '" . $job1 . "' or job = '" . $job2 . "' or job = '" . $job3 . "' or job = '" . $job4 . "' or job = -1 ) order by UpdateTime DESC;";
			}else
			{
				$sql = "SELECT TOP " . $limit . " * FROM ItemInfo WHERE id_x NOT IN(SELECT TOP " . $start . " id_x FROM ItemInfo WHERE Status = 1) AND Status = 1 order by UpdateTime DESC;";
			}
        } else {
			if ( $job != -1 )
			{
				$sql = "SELECT TOP " . $limit . " * FROM ItemInfo WHERE Type = '" . $type . "' AND id_x NOT IN(SELECT TOP " . $start . " id_x FROM ItemInfo WHERE Type = '" . $type . "' AND Status = 1) AND Status = 1 AND (job = '" . $job1 . "' or job = '" . $job2 . "' or job = '" . $job3 . "' or job = '" . $job4 . "' or job = -1 ) order by UpdateTime DESC;";
			}else{
				$sql = "SELECT TOP " . $limit . " * FROM ItemInfo WHERE Type = '" . $type . "' AND id_x NOT IN(SELECT TOP " . $start . " id_x FROM ItemInfo WHERE Type = '" . $type . "' AND Status = 1) AND Status = 1 order by UpdateTime DESC;";
			}
        }
        return $this->dbc->fetchData($sql);
    }

    /**
     * 获得一批商品编号对应的商品信息
     * @param type $itemNos
     * @return type
     */
    public function getItemByNo($itemNos) {
        $sql = "SELECT * FROM ItemInfo WHERE ItemNo IN(" . $itemNos . ") AND Status = 1;";
        return $this->dbc->fetchData($sql);
    }

    /**
     * 根据商品ID获得商品信息
     * @param type $id
     * @return type
     */
    public function getItemById($id) {
        $sql = "SELECT * FROM ItemInfo WHERE id_x = " . $id;
        return $this->dbc->fetchData($sql);
    }

    /**
     * 根据类型获得总的商品数量
     * @param type $type
     * @return type
     */
    public function getItemNumber($type) {
        if ($type == "all" || $type == "") {
            $sql = "SELECT COUNT(id_x) AS totalNumber FROM ItemInfo WHERE Status = 1;";
        } else {
            $sql = "SELECT COUNT(id_x) AS totalNumber FROM ItemInfo WHERE Type = '" . $type . "' AND  Status = 1;";
        }

        $data = $this->dbc->fetchData($sql);
        if (isArrayNotEmpty($data)) {
            return $data[0]['totalNumber'];
        }
        return 0;
    }

    /**
     * 修改商品
     * @param type $idx
     * @param type $itemNo
     * @param type $itemName
     * @param type $price
     * @param type $type
     * @param type $imageUrl
     * @return type
     */
    public function modifyItem($idx, $itemNo, $itemName, $price, $type, $imageUrl = "") {
        if ($imageUrl == "") {
            $sql = "UPDATE ItemInfo SET ItemNo = '" . $itemNo . "',ItemName = '" . $itemName
                    . "',Price = '" . $price . "',Type = '" . $type . "',UpdateTime = getDate() FROM ItemInfo WHERE id_x = " . $idx . ";";
        } else {
            $sql = "UPDATE ItemInfo SET ItemNo = '" . $itemNo . "',ItemName = '" . $itemName
                    . "',Price = '" . $price . "',Type = '" . $type . "',UpdateTime = getDate(),ImageUrl = '" . $imageUrl . "' FROM ItemInfo WHERE id_x = " . $idx . ";";
        }
        return $this->dbc->execute($sql);
    }

    /**
     * 新增
     * @param type $itemNo
     * @param type $itemName
     * @param type $price
     * @param type $type
     * @param type $imageUrl
     * @return type
     */
    public function createItem($itemNo, $itemName, $price, $type, $imageUrl = "") {
        $sql = "insert into ItemInfo(ItemNo,ItemName,Price,Type,UpdateTime,ImageUrl,status) values('" . $itemNo . "','" . $itemName
                . "','" . $price . "','" . $type . "', getDate(),'" . $imageUrl . "',1)";
        return $this->dbc->execute($sql);
    }

    /**
     * 删除商品（状态改成下线）status 状态：1=上线  0=下线
     * @param type $idx 
     * @return type
     */
     public function deleteItem($idx) {
        $sql = "UPDATE ItemInfo SET Status = 0 WHERE id_x = " . $idx . ";";
        return $this->dbc->execute($sql);
    }
}
